﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using System.Runtime.InteropServices;
namespace Tesla.Math {

    /// <summary>
    /// Defines a 3 point primitive.
    /// </summary>
    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public struct Triangle {
        private Vector3 _pointA;
        private Vector3 _pointB;
        private Vector3 _pointC;
        private Vector3 _center;
        private Vector3 _normal;

        /// <summary>
        /// Gets or sets the first vertex.
        /// </summary>
        public Vector3 PointA {
            get {
                return _pointA;
            }
            set {
                _pointA = value;
                CalculateCenter();
                CalculateNormal();
            }
        }

        /// <summary>
        /// Gets or sets the second vertex.
        /// </summary>
        public Vector3 PointB {
            get {
                return _pointB;
            }
            set {
                _pointB = value;
                CalculateCenter();
                CalculateNormal();
            }
        }

        /// <summary>
        /// Gets or sets the third vertex.
        /// </summary>
        public Vector3 PointC {
            get {
                return _pointC;
            }
            set {
                _pointC = value;
                CalculateCenter();
                CalculateNormal();
            }
        }

        /// <summary>
        /// Gets the normal vector, which describes the direction
        /// the triangle is facing. This is determined by the "winding order" of the
        /// points.
        /// </summary>
        public Vector3 Normal {
            get {
                return _normal;
            }
        }

        /// <summary>
        /// Gets the center of the triangle.
        /// </summary>
        public Vector3 Center {
            get {
                return _center;
            }
        }

        /// <summary>
        /// Creates a new triangle with the three vertex locations.
        /// </summary>
        /// <param name="a">Point A</param>
        /// <param name="b">Point B</param>
        /// <param name="c">Point C</param>
        public Triangle(Vector3 a, Vector3 b, Vector3 c) {
            _pointA = a;
            _pointB = b;
            _pointC = c;
            _normal = Vector3.Zero;
            _center = Vector3.Zero;
            CalculateCenter();
            CalculateNormal();
        }

        /// <summary>
        /// Sets the three vertex locations of the triangle.
        /// </summary>
        /// <param name="a">Point A</param>
        /// <param name="b">Point B</param>
        /// <param name="c">Point C</param>
        public void Set(Vector3 a, Vector3 b, Vector3 c) {
            _pointA = a;
            _pointB = b;
            _pointC = c;
            CalculateCenter();
            CalculateNormal();
        }

        private void CalculateNormal() {
            Vector3 edge1;
            Vector3 edge2;
            Vector3.Subtract(ref _pointC, ref _pointC, out edge1);
            Vector3.Subtract(ref _pointB, ref _pointA, out edge2);

            Vector3 cross;
            Vector3.Cross(ref edge1, ref edge2, out cross);
            Vector3.Normalize(ref cross, out _normal);
        }

        private void CalculateCenter() {
            Vector3 temp;
            Vector3.Add(ref _pointA, ref _pointB, out temp);
            Vector3.Add(ref _pointC, ref temp, out temp);
            Vector3.Multiply(ref temp, MathHelper.OneThird, out _center);
        }
    }
}
